#/*************************************************************************
#    > File Name: examples/example_File.py
#    > Author: Yan Wang
#    > Mail: wangyan@imnu.edu.cn
#    > Created Time: 2022年06月22日 星期三 15时04分55秒
# ************************************************************************/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys   
sys.path.append('./Lib/')   
from Load_Lib import *  

print("The examples in this file are:")   
ex_name={"1":"Find_Line"
        ,"2":"Extract_One_Sentence_From_MultiFile"
        ,"3":"Replace_Multi_File"
        ,"4":"File_Replace_with_Para"
        ,"all":"all examples"}    #字典，所有对应函数为Lib/Content_Operation.py
for key in ex_name:
    print(key, ex_name[key])       #if条件语句，如果key在ex_name里面，则输出eg：1 Find_Line
num=input("请输入想要运行的例子：") 


if num == "1" or num == ex_name["1"] or num == "all": 
    # Find_Line   
# 两参数(A,B),A为源文件,B为label
# 在path_name中找到labels中的value并且往下替换key行,输出为 word_list 为需要被替换的行的内容
# 最后输出为一个列表,其中为被替换的为8,9,10,11行
    path_name="./examples/test_input/test_file.dat"
    labels={"DATA_FOLDER      : 'data'":4} #此内容在原文第7行

    #Exhibit_Function(Find_Line, [path_name, labels])
    # 输出为第 8,9,10,11行的内容。

#  同样地,在log_file中找到MGparam_label_value中的value并且往下替换key行,输出为 word_list 为需要被替换的行的内容
    log_file="/home/yancy/Software/MG5/sba_tb_20/THDM_CH_4b0j_1/Events/run_18/run_18_tag_1_banner.txt"
    MGparam_label_value={"BLOCK FRBLOCK":2,"23 9.118760e+01 # mz":4}
    word_list, input_line_list = Find_Line(log_file, MGparam_label_value)
    print("word_list",log_file, word_list, input_line_list)




if num == "2" or num == ex_name["2"] or num == "all":
    # Extract_One_Sentence_From_MultiFile
    print("")
    print("正在查看 Extract_One_Sentence_From_MultiFile函数")
    print("函数包含四个参数(A,B,line_num=0,ignored=[\".lhe\",\".root\",\".gz\",\".hepmc\",\".tar\",\".deb\"]),A为给定文件夹,B为被寻找的字符串,ignored为跳过的文件扩展名")
    print("查找 A 文件夹下所有的文件是否包含B的字符串")
    print("测试代码为 target_folder=\"./Examples/test_input/\", labels=\"DATA_FOLDER\"") 
    print("最后输出为:")
    print("['./examples/test_input/test.yaml', './examples/test_input/test_file.dat']")
    print("[\"DATA_FOLDER      : 'data'\", \"DATA_FOLDER      : 'data'\"]")
    print("即找到./examples/test_input/文件夹下的所有包含的DATA_FOLDER字符串的文件，并且输出它们所在的文件(绝对路径)")
    target_folder="./Examples/test_input/"
    labels="DATA_FOLDER" 

    Exhibit_Function(Extract_One_Sentence_From_MultiFile, [target_folder, labels])


if num == "3" or num == ex_name["3"] or num == "all":
    # Replace_Multi_File
# 四个参数(A,B,C,D),A为字典包含(value,key),B为最后替换的输出文件,C为被替换的源文件，D为被替换的内容
# 多个文件进行替换，用于替换param_card为多个test的文件
# 最后生成为 ./examples/test_output/test_1.dat和./examples/test_output/test_2.dat两个文件,这两个文件中的labels内容已经被input_para中的内容替换
    #labels={"Block frblock":2,"23 9.118760e+01 # MZ ":4}
    labels={"23 9.118760e+01 # mz":4,"BLOCK THDMBLOCK #":3}
    #files=["./Examples/test_output/test_1.dat", "./Examples/test_output/test_2.dat"]
    files=["./Examples/test_output/test_1.dat"]
    #source="./Examples/test_input/param_card.dat"
    source="./Examples/test_input/param_card_TYPEI.dat"
    input_para=[['      25 110.0 # mhl',  '      135 125.0 #mhh',  '      136 109.0 #mha', '      137 300.0 #mhp',
                 '      1 10.25 # tb', '      2 0.11 # cba', '      3 130.0 # m122']]
    '''
    input_para=[['    1 scan1: [20.663656, 10.436171] # TB', 
        '    2 scan1: [-0.052205, -0.10005] # sinbma',
        '   25 scan1: [28.93582, 27.506943] # mh1',
        '   35 scan1: [125.0, 125.0] # mh2',
        '   36 scan1: [82.515336, 117.351299] # mh3',
        '   37 scan1: [85.577868, 93.766289] # mhc'], 
        ['    1 scan1: [20.805261, 16.394238] # TB',
            '    2 scan1: [-0.056699, -0.070081] # sinbma',
            '   25 scan1: [53.510544, 50.509354] # mh1',
            '   35 scan1: [125.0, 125.0] # mh2',
            '   36 scan1: [96.121403, 104.910123] # mh3',
            '   37 scan1: [111.671804, 121.010468] # mhc']]
   '''
    Copy_File(source,files[0])
#    Copy_File(source,files[1])
    Exhibit_Function(Replace_Multi_File, [labels, files, source, input_para])


if num == "4" or num == ex_name["4"] or num == "all":
    # File_Replace_with_Para 
# 四个参数(A,B,C,D),A为被替换的文件,B为被替换源文件的文件扩展(生成的多个被替换的文件),C为目标文件夹(目录),D被替换的那些字典内容
# 整合多个替换相关函数,用于给定替换模式下,生成多个被替换的文件
# 最后输出为一个./examples/test_output/control_scan_4b0j_general/control_ARCH/path_{1--5}.dat文件,其中的内容已被替换
    cut_cross=[19.53, 66.25, 17.97, 0.6, 19.43]
    replaced_param_list=[
            ['PROCESS          : sigh_w4b_1', "RECORD_FILE      : 'tmp/sigh_w4b_1.dat'", "INPUT_FILE       : 'file_1.dat'"], 
            ['PROCESS          : sigh_w4b_2', "RECORD_FILE      : 'tmp/sigh_w4b_2.dat'", "INPUT_FILE       : 'file_2.dat'"], 
            ['PROCESS          : sigh_w4b_3', "RECORD_FILE      : 'tmp/sigh_w4b_3.dat'", "INPUT_FILE       : 'file_3.dat'"], 
            ['PROCESS          : sigh_w4b_4', "RECORD_FILE      : 'tmp/sigh_w4b_4.dat'", "INPUT_FILE       : 'file_4.dat'"], 
            ['PROCESS          : sigh_w4b_5', "RECORD_FILE      : 'tmp/sigh_w4b_5.dat'", "INPUT_FILE       : 'file_5.dat'"]
            ]
    file_source="path.dat"
    source_folder= './examples/test_input/control_ARCH'
    target_folder= './examples/test_output/control_scan_4b0j_general'
    Copy_File(source_folder,target_folder)   #copy多个source_folder目标文件内的内容复制到target_folder中并依次命名_1/_2.....
    path_labels={"PROCESS          : sigh_w4b_1":0,"RECORD_FILE      : 'tmp/sigh_w4b_1.dat'":0, "INPUT_FILE       : 'file_sigh_w4b_1.dat'":0}
    Exhibit_Function(File_Replace_with_Para,[replaced_param_list, file_source,target_folder, path_labels])
